package com.si.anddos.filechooser;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;

import com.si.anddos.R;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class FileFolderChooser extends Dialog implements OnItemClickListener, OnClickListener {

	private ListView mFolders;
	private TextView mCurrentFolder;
	private Folder mPath;
	private Folder mFilePath;
	private File mRootSDCard;
	private FolderAdapter mAdapter;
	private OnClickListener mListener;
	private boolean mAcceptFiles;
	private View mOkButton;

	public FileFolderChooser(Context context, String startPath, OnClickListener listener, int themeId) {
		this(context, startPath, listener, themeId, false);
	}

	public FileFolderChooser(Context context, String startPath, OnClickListener listener, int themeId, boolean acceptFiles) {
		super(context, themeId);
		mListener = listener;
		mAcceptFiles = acceptFiles;
		setTitle(acceptFiles? R.string.titleChooseFile : R.string.titleChooseFolder);
		setContentView(R.layout.folders);

		mRootSDCard = Environment.getExternalStorageDirectory();
		
		mOkButton = findViewById(R.id.ok_btn);
		mOkButton.setOnClickListener(this);
		findViewById(R.id.cancel_btn).setOnClickListener(this);
		mCurrentFolder = (TextView) findViewById(R.id.textCurrentFolder);
		mCurrentFolder.setSelected(true);
		mFolders = (ListView) findViewById(R.id.listFolders);
		mFolders.setOnItemClickListener(this);

				
		mAdapter = new FolderAdapter();
		mFolders.setAdapter(mAdapter);
		mPath = new Folder(startPath);
		updateAdapter();
	}

	public String getPath() {
		if (!mAcceptFiles) {
			return mPath.getAbsolutePath();
		} else
		if (mFilePath != null) {
			return mFilePath.getAbsolutePath();
		} else {
			return null;
		}
	}

	public void onClick(View v) {
		if (v == mOkButton && mListener != null) {
			mListener.onClick(this, DialogInterface.BUTTON_POSITIVE);
		}
		dismiss();
	}

	private void updateAdapter() {
		mCurrentFolder.setText(mPath.getAbsolutePath());
		mAdapter.clear();
		if (!mPath.equals(mRootSDCard)) {
			mAdapter.add(new Folder(mPath, true));
		}
		File[] dirs = mPath.listFiles(mDirFilter);
		if(dirs != null){
			Arrays.sort(dirs);
			for (int i = 0; i < dirs.length; i++) {
				if(!dirs[i].getName().startsWith("."))
					mAdapter.add(new Folder(dirs[i]));
			}
		}
		if (mAcceptFiles) {
			File[] files = mPath.listFiles(mFileFilter);
			if(files != null){
				Arrays.sort(files);
				for (int i = 0; i < files.length; i++) {
					if(!files[i].getName().startsWith("."))
						mAdapter.add(new Folder(files[i]));
				}
			}
		}
		mAdapter.notifyDataSetChanged();
		mFolders.setSelection(0);
	}

	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		if (mAcceptFiles) {
			Folder item = (Folder) mAdapter.getItem(position);
			if(item != null && item.exists()){
				if (item.isDirectory()) {
					mPath = item;
					updateAdapter();
					mFilePath = null;
				} else {
					mCurrentFolder.setText(item.getAbsolutePath());
					mFilePath = item;
				}
			}
		} else {
			mPath = (Folder) mAdapter.getItem(position);
			if( mPath != null && mPath.exists())
				updateAdapter();
		}
	}

	private FileFilter mDirFilter = new FileFilter() {
		public boolean accept(File file) {
			return file.isDirectory();
		}
	};

	private FileFilter mFileFilter = new FileFilter() {
		public boolean accept(File file) {
			return file.isFile();
		}
	};
	
	class FolderAdapter extends BaseAdapter {
		ArrayList<Folder> mFolders = new ArrayList<Folder>();
		LayoutInflater mInflater = LayoutInflater.from(getContext());
		private Drawable mFolderDrawable;
		private Drawable mFileDrawable;
		
		public FolderAdapter() {
			Resources res = getContext().getResources();
			mFolderDrawable = res.getDrawable(R.drawable.folder);
			mFileDrawable = res.getDrawable(R.drawable.file);
		}
		
		public int getCount() {
			return mFolders.size();
		}

		public void add(Folder folder) {
			mFolders.add(folder);
		}

		public void clear() {
			mFolders.clear();
		}

		public Object getItem(int position) {
			return mFolders.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View v = mInflater.inflate(R.layout.folder, parent, false);
			Folder folder = mFolders.get(position);
			TextView name = (TextView) v.findViewById(R.id.textFolderName);

			Drawable drawable = null;
			if (folder.isParent) {
				name.setText("[..]");
				drawable = mFolderDrawable;
			} else {
				name.setText(folder.getName());
				if (folder.isDirectory()) {
					drawable = mFolderDrawable;
				} else {
					drawable = mFileDrawable;
				}
			}
			v.findViewById(R.id.imageFolderIcon).setBackgroundDrawable(drawable);
			return v;
		}
	}
	
	
	
	@SuppressWarnings("serial")
	class Folder extends File {
		private boolean isParent;

		public Folder(File file) {
			super(file.getAbsolutePath());
		}

		public Folder(File file, boolean unused) {
			super(file.getParent());
			isParent = true;
		}
		
		public Folder(String path) {
			super(path);
		}
	}
}
